﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Training.Dia1.Data.Managers;
using Training.Dia1.Data.Entities;
using System.Linq;

namespace Training.Dia1.Data.Test.Data
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestGetMeasureUnitList()
        {
            
            var lista = MeasureUnitManager.GetList("");
            Assert.IsTrue(lista.Count > 0);
            
        }
        [TestMethod]
        public void TestAddMeasureUnit()
        {
            MeasureUnit measureUnit = new MeasureUnit()
            {
                Name = "LITROS" + DateTime.Now,
                Abbreviation = "LT",
            };
            MeasureUnit measureUnitDB = MeasureUnitManager.Create(measureUnit);
            Assert.IsNotNull(measureUnitDB.Id);
        }

        [TestMethod]
        public void TestUpdateMeasureUnit()
        {
            MeasureUnit measureUnitDB = MeasureUnitManager.GetList("").First();
            string Name = measureUnitDB.Name;
            measureUnitDB.Name = "HOLA " + DateTime.Now;

            MeasureUnit r = MeasureUnitManager.Update(measureUnitDB);
            Assert.AreNotEqual(Name, r.Name);
        }

        [TestMethod]
        public void TestGetByIdMeasureUnit()
        {
            Guid measureUnitId = MeasureUnitManager.GetList("").First().Id;
            MeasureUnit r = MeasureUnitManager.GetById(measureUnitId);
            Assert.IsNotNull(r.Name);
        }

        [TestMethod]
        [ExpectedException(typeof(Exception))]
        public void TestGetByIdMeasureUnitException()
        {
            Guid measureUnitId = new Guid("15972402-76B0-E411-BE67-74DE2B991954");
            MeasureUnit r = MeasureUnitManager.GetById(measureUnitId);

        }

        [TestMethod]
        public void TestDeleteMeasureUnit()
        {
            Guid measureUnitId = MeasureUnitManager.GetList("").First().Id;
            MeasureUnitManager.Delete(measureUnitId);
            MeasureUnit r = GetById(measureUnitId);
            Assert.IsNull(r);
        }
        private MeasureUnit GetById(Guid Id)
        {
            using (TrainingDia1DbContext ctx = new TrainingDia1DbContext())
            {
                return ctx.MeasureUnits.Find(Id);
            }
        }



    }
}
